const http = "https://runen.unohacha.com/Api/api"
//const http = "https://huaan.unohacha.com/Api/api"
const imgUrl = "/images/"
// post请求
function _post(url, data, success, fail) {
    var data = data;
    data.token = wx.getStorageSync('token');
    wx.request({
        url: http + '/' + url,
        header: {
            'content-type': 'application/x-www-form-urlencoded',
        },
        method: 'POST',
        data: data,
        success(res) {
            console.log(res.data)
            if (res.data.re === '-1') {
                // wx.showToast({
                //     title: res.data.info,
                //     icon: 'none',
                //     duration: 3000
                // })
                // setTimeout(function() {
                //     wx.reLaunch({
                //         url: '/pages/login/loginP/loginP',
                //     })
                // }, 800)
              wx.reLaunch({
                url: '/pages/login/loginP/loginP',
              })
            } else if (res.data.re === '0'){
                  wx.showToast({
                    title: res.data.info,
                    icon: 'none',
                    duration: 3000
                  })
            }
            
            else {
                success(res)
            }
        },
        fail(err) {
            fail(err)
        }
    })
}
// 判断网络状态
function onNetworkStatusChangeFn() {
    
}
// 登录
function loginFn() {
    let nowSDKVersion = wx.getSystemInfoSync().SDKVersion
    if (compareVersion(nowSDKVersion, '1.4.0') == -1) {
        wx.showToast({
            title: '您的微信基础版本库过低，请升级微信或使用原版微信进行体验！',
            icon: 'none',
            duration: 3000
        })
        return
    }
    wx.login({
        success(res) {
            let code = res.code
        }
    })
}

// 获取data-index 属性
function dataIndex(e) {
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let type_data = e.currentTarget.dataset.type
    let name = e.currentTarget.dataset.name
    let alldata = e.currentTarget.dataset.alldata
    let url = e.currentTarget.dataset.url

    let dataIndex = [index, id, type_data, name, alldata, url]
    return dataIndex
}

// 获取input框里面所有的数据
function inputArr(arr, e) {
    let index = dataIndex(e)[0]
    let temp_arr = arr
    let temp_val = e.detail.value
    temp_arr[index] = temp_val
    return temp_arr
}

// 输入为空
function ifEmpty(val) {
    if (!val || !val.replace(/\s+/g, "")) {
        return false
    } else {
        return true
    }
}

// 基本正则验证
function rexFn(str) {
    var rex1 = /^1\d{10}$/ // 手机的正则匹配
    var rex2 = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/ // 身份证正则匹配
    var rex3 = /^[\u4e00-\u9fa5]+$/ // 汉字匹配
    var falgArr = []
    if (rex1.test(str)) {
        falgArr[0] = true
    } else if (rex2.test(str)) {
        falgArr[1] = true
    } else if (rex3.test(str)) {
        falgArr[2] = true
    }
    return falgArr
}

// 版本比较
function compareVersion(v1, v2) {
    v1 = v1.split('.')
    v2 = v2.split('.')
    let len = Math.max(v1.length, v2.length)

    while (v1.length < len) {
        v1.push('0')
    }
    while (v2.length < len) {
        v2.push('0')
    }

    for (let i = 0; i < len; i++) {
        let num1 = parseInt(v1[i])
        let num2 = parseInt(v2[i])

        if (num1 > num2) {
            return 1
        } else if (num1 < num2) {
            return -1
        }
    }

    return 0
}

// 返回
function goBack() {
    wx.navigateBack({
        delta: 1
    })
}

// 获取html页面中的body部分
let REG_BODY = /<body[^>]*>([\s\S]*)<\/body>/

function getBody(content) {
    let result = REG_BODY.exec(content)
    if (result && result.length === 2)
        return result[1]
    return content
}

module.exports = {
    http,
    imgUrl,
    _post,
    loginFn,
    dataIndex,
    inputArr,
    ifEmpty,
    rexFn,
    compareVersion,
    goBack,
    getBody,
    onNetworkStatusChangeFn
}